<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/12/8
 * @Description:用户设置状态
 */

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

class ImUserCondition extends BaseModel
{
    protected $table = 'im_user_condition';
    protected $hidden = ['created_at','updated_at'];


    //关联状态
    public function status()
    {
        return $this->belongsTo(ImCondition::class, 'condition_id', 'id')
            ->select(['id', 'img', 'note', 'type']);
    }


    /**
     * @desc 设置用户的状态，24小时有效
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/8 14:58
     */
    public function setCondition($input, $userId)
    {
        $userCondition = $this->where(['user_id' => $userId])->first();
        if ($userCondition)
        {
            $userCondition->condition_id = $input['id'];
            $userCondition->expired_at = time() + 24 * 60 * 60;
            $userCondition->save();
        }
        else
        {
            $this->condition_id = $input['id'];
            $this->user_id = $userId;
            $this->expired_at = time() + 24 * 60 * 60;
            $this->save();
        }

        return ['code' => 0, 'msg' => '设置成功'];
    }




}
